﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Mozhi.Abp.Core.Extensions
{
    public static class DecimalExtension
    {
        public static decimal ToDecimal(this decimal? value)
        {
            if (!value.HasValue)
                return 0;

            return value.Value;
        }

        public static decimal ToDecimal(this decimal? value, int precision)
        {
            if (value == null)
                return 0.00M;

            precision = precision < 2 ? 2 : precision;

            return Math.Round(value.Value, precision, MidpointRounding.AwayFromZero);
        }

        public static decimal ToDecimal(this decimal value, int precision)
        {
            precision = precision < 2 ? 2 : precision;

            return Math.Round(value, precision, MidpointRounding.AwayFromZero);
        }

        public static decimal ToMoney(this decimal value)
        {
            return Math.Round(value, 2, MidpointRounding.AwayFromZero);
        }

        public static decimal ToMoney(this decimal? value)
        {
            if (value == null)
                return 0;

            return Math.Round(value.Value, 2, MidpointRounding.AwayFromZero);
        }

        public static decimal ToPrice(this decimal? value)
        {
            if (!value.HasValue)
                return 0;

            return ToPrice(value.Value);
        }

        public static decimal ToPrice(this decimal value)
        {
            return Math.Round(value, 6, MidpointRounding.AwayFromZero);
        }
    }
}
